/*
 * JOGRE (Java Online Gaming Real-time Engine) - TicTacToe
 * Copyright (C) 2004  Bob Marks (marksie531@yahoo.com)
 * http://jogre.sourceforge.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.jogre.mayi.common;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import nanoxml.XMLElement;

import org.jogre.common.comm.Comm;
import org.jogre.common.comm.CommGameOver;
import org.jogre.common.comm.CommTableMessage;

/**
 * Communications object for transmitting a card for spades.
 *
 * @author Garrett Lehman (Gman)
 * @version Alpha 0.2.3
 */
public class CommMayIScorecard extends CommTableMessage {

	// xml information
	public static final String XML_NAME = "mayi_scorecard";

	private List<CommMayIHandScore> scores;
	
	private CommGameOver gameOver = null;

	/**
	 * Constructor that takes a username and card object.
	 *
	 * @param username
	 *            Username
	 * @param card
	 *            Card
	 */
	public CommMayIScorecard(List<CommMayIHandScore> scores) {
		super();
		this.scores = scores;
	}

	/**
	 * Constuctor that takes an xml element and creates an object from that xml
	 * element.
	 *
	 * @param message
	 *            Xml element
	 */
	public CommMayIScorecard(XMLElement message) {
		super(message);

		this.scores = new ArrayList<CommMayIHandScore>();
		
		// Read child elements - card objects
		Enumeration e = message.enumerateChildren();
		while (e.hasMoreElements()) {
			XMLElement childMessage = (XMLElement) e.nextElement();

			if (childMessage.getName().equals(CommMayIHandScore.XML_NAME)) {
				this.scores.add(new CommMayIHandScore(childMessage));
			}
			else if (childMessage.getName().equals(Comm.GAME_OVER)) {
				this.gameOver = new CommGameOver(childMessage);
			}
		}
	}

	

	public CommGameOver getGameOver() {
		return gameOver;
	}

	public void setGameOver(CommGameOver gameOver) {
		this.gameOver = gameOver;
	}

	public List<CommMayIHandScore> getScores() {
		return scores;
	}

	/**
	 * Flattens this object into xml.
	 *
	 * @see org.jogre.common.comm.ITransmittable#flatten()
	 */
	public XMLElement flatten() {
		XMLElement message = super.flatten(XML_NAME);
		for (CommMayIHandScore score : this.scores) {
			message.addChild(score.flatten());
		}
		if (this.gameOver != null) {
			message.addChild(this.gameOver.flatten());
		}
		return message;
	}
}